<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <?php
  //获取学号
  if ($_POST['stuno']) {
    $stuno = $_POST['stuno'];
  } else {
    $stuno = '没有填写';
  }

  //获取姓名
  if ($_POST['stuname']) {
    $stuname = $_POST['stuname'];
  } else {
    $stuname = '没有填写';
  }

  //获取密码
  if ($_POST['pwd']) {
    $password = $_POST['pwd'];
  } else {
    $password = '没有填写';
  }

  //获取班级
  $classname = $_POST['classname'];

  //获取性别
  $sex = $_POST['sex'];

  //获取爱好
  if (array_key_exists('hobby', $_POST)) {
    $hobby = join(',', $_POST['hobby']);
  } else {
    $hobby = '没有任何爱好 ';
  }

  //获取手机号
  if ($_POST['mobile']) {
    $mobile = $_POST['mobile'];
  } else {
    $mobile = '没有填写';
  }

  //获取邮箱
  if ($_POST['email']) {
    $email = $_POST['email'];
  } else {
    $email = '没有填写';
  }

  switch ($_FILES['photo']['error']) {
    case 0: //成功上传
      $ftypes = ['image/gif', 'image/pjpeg', 'image/jpeg', 'image/png'];
      $type = $_FILES['photo']['type'];
      if (in_array($type, $ftypes))  //上传的文件是指定的类型
      {
        $fname = $_FILES['photo']['name'];  //上传的原始文件名
        $tmp = explode('.', $fname); //将文件名以"."分隔成两部分  
        $newfname = $stuno . '.' . $tmp[1]; //新的文件名为学号              
        $destination = 'upload/' . $newfname;
        move_uploaded_file($_FILES['photo']['tmp_name'], $destination);
      } else {
        $txt = '上传文件类型不符合要求';
      }
      break;
    case 1: //文件大小超过了PHP默认的限制2MB
      $txt = '上传文件出错，文件大小超过了限制！';
      break;
    case 4: //没有选择上传文件
      $txt = '没有上传照片！';
      break;
  }

  // /Applications/MAMP/htdocs
  $document_root = $_SERVER['DOCUMENT_ROOT'];
  echo "站点根目录：" . $document_root;
  // fopen(path_string, mode: a); 打开文件
  // fwrite($file, $string);  在文件中写入内容
  // fclose($file); 关闭文件
  $file = fopen("$document_root/classwork/10-27/info.txt", 'a');

  // info.txt中写入的信息
  $string = "\n" .
    "学号：" . $stuno . "\r\n" .
    "姓名：" . "$stuname" . "\r\n" .
    "密码：" . "$password" . "\r\n" .
    "班级：" . "$classname" . "\r\n" .
    "性别：" . "$sex" . "\r\n" .
    "爱好：" . "$hobby" . "\r\n" .
    "手机：" . "$mobile" . "\r\n" .
    "邮件：" . "$email" . "\r\n" .
    "照片：" . "$destination" . "\r\n";

  // $string是我们写入的内容
  fwrite($file, $string);
  fclose($file);
  ?>

  <h1>注册成功，您的注册信息如下：</h1>
  学号：<?php echo $stuno ?>
  <br>
  姓名：<?php echo $stuname ?>
  <br>
  密码：<?= $password ?>
  <br>
  班级：<?= $classname ?>
  <br>
  性别：<?= $sex ?>
  <br>
  爱好：<?= $hobby ?>
  <br>
  手机：<?= $mobile ?>
  <br>
  邮箱：<?= $email ?>
  <br>
  相片：<?php
      if (isset($txt)) {
        echo $txt;    //输出错误提示
      } else {
        echo "<img src='$destination'  style='width:150px;height:120px;'>";
      }
      ?>
      <?php
        $file=fopen("$document_root/classwork/10-27/info.txt", 'r');
        $fileSize=filesize("$document_root/classwork/10-27/info.txt");
        $content=fread($file,$fileSize);
        echo nl2br($content); // 在字符串所有新行之前插入
        while (!feof($file)) {
          $str=fgets($file);
          if (strstr($str, '照片:'))
          {
            $path=substr($str,7,-1);
            echo "照片：<img src='$path' style='width:150px;height:120px;' >";
          } else
          {
            echo nl2br($str);
          }
        }
      ?>
</body>

</html>